﻿using System;
using System.Collections.Generic;
using HK.Core.Common.Data;
using HK.Core.Common.Protocol;
using HK.Core.Common.Protocol.Data;
using HK.Core.Extend;
using HK.Core.Serializable;
using HK.Core.Serializable.Data;
using HK.Core.Serializable.Protocol.Data;
using HK.Editor.Serializable.Protocol;
using UnityEditor;
using UnityEngine;

namespace HK.Editor.Serializable
{
    /// <summary>
    /// 序列化文件编辑器 - 列表
    /// </summary>
    /// <typeparam name="TAsset">序列化资产文件类型</typeparam>
    /// <typeparam name="TBody">列表Body数据类型</typeparam>
    /// <typeparam name="TElement">列表Element数据类型</typeparam>
    public abstract class SAssetListEditor<TAsset, TBody, TElement> : SAssetEditor<TAsset>, ISAssetListEditor<TAsset, TBody, TElement>
        where TAsset : SAsset, ISAssetList<TBody, TElement>
        where TBody : SAssetListData<TBody, TElement>
        where TElement : JsonData, IListElement, IEquatable<TElement>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        protected SAssetListEditor() : base() {}
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="iDefaultInspector"></param>
        protected SAssetListEditor(bool iDefaultInspector) : base(iDefaultInspector) {}
        
        /// <summary>
        /// 序列化文件数据 - Body
        /// </summary>
        public TBody Body => Self.Body;

        /// <summary>
        /// 列表
        /// </summary>
        public List<TElement> List => Body.List;

#region SerializedProperty

        /// <summary>
        /// List Property
        /// </summary>
        private SerializedProperty _listProperty = null;

        /// <summary>
        /// 初始化可序列化对象
        /// </summary>
        protected override bool InitSerializedProperties()
        {
            if (!base.InitSerializedProperties()) return false;
            
            _listProperty = FindSerializedProperty(BodyProperty, "list");
            if (null == _listProperty)
            {
                this.Error("InitSerializedProperties():There is no property named list in body!");
            }
            return null != _listProperty;
        }

#endregion

#region Draw

#region Body

        /// <summary>
        /// 绘制Body
        /// </summary>
        /// <param name="iEvent">Event</param>
        /// <param name="iProperty">Property</param>
        protected override void DrawBody(Event iEvent, SerializedProperty iProperty)
        {
            if(null == iProperty) return;
                            
            var bodyBgRect = EditorGUILayout.GetControlRect(false, GetBodyHeight(iProperty), EditorStyles.largeLabel);
            if (iProperty.isExpanded)
            {
                bodyBgRect.yMax -= 1;
            }
            else
            {
                bodyBgRect.height = HeaderHeight;
            }

            // 背景
            bodyBgRect = DrawGenericBg(bodyBgRect, iEvent);
                            
            // 绘制
            var bodyRect = bodyBgRect;
            //bodyRect.yMin += 1;
            bodyRect.yMax -= 1;
            EditorGUI.PropertyField(bodyRect, iProperty);
        }

#endregion

#endregion
        
    }
}